#ifndef CH395_CMD_H
#define CH395_CMD_H
#include "example.h"

//----------------------------------------------------
//- ch395 协议类型
//----------------------------------------------------
#define PROTO_TYPE_IP_RAW                         0                             
#define PROTO_TYPE_MAC_RAW                        1                             
#define PROTO_TYPE_UDP                            2                             
#define PROTO_TYPE_TCP                            3                             

//----------------------------------------------------
//- ch395 配置
//----------------------------------------------------
typedef struct {
  uint8_t  local_ip[4];          //本地ip              
  uint8_t  local_mask[4];        //本地掩码        
  uint8_t  local_getway[4];      //本地网关      
  uint16_t local_port;           //本地端口  
  uint8_t  remote_ip[4];         //目标ip  
  uint16_t remote_port;          //目标端口  
  uint8_t  prototype;            //协议类型 udp   tcp
}ifconfig,*pifconfig;

//----------------------------------------------------
//- ch395 指令列表
//----------------------------------------------------
#define CMD01_GET_IC_VER                          0x01                                                                                                                                 
#define CMD31_SET_BAUDRATE                        0x02                                                                                                
#define CMD00_ENTER_SLEEP                         0x03                                                                
#define CMD00_RESET_ALL                           0x05                                                                                
#define CMD11_CHECK_EXIST                         0x06                                                  
#define CMD02_GET_GLOB_INT_STATUS_ALL             0x19                                                                                             
#define CMD10_SET_PHY                             0x20                                                 
#define CMD60_SET_MAC_ADDR                        0x21                                                                   
#define CMD40_SET_IP_ADDR                         0x22                                                              
#define CMD40_SET_GWIP_ADDR                       0x23                                                                   
#define CMD40_SET_MASK_ADDR                       0x24                                                                     
#define CMD90_SET_MAC_FILT                        0x25                          
#define CMD01_GET_PHY_STATUS                      0x26                                                         
#define CMD0W_INIT_CH395                          0x27                          
#define CMD08_GET_UNREACH_IPPORT                  0x28                          
#define CMD01_GET_GLOB_INT_STATUS                 0x29                                                       
#define CMD10_SET_RETRAN_COUNT                    0x2A                                     
#define CMD20_SET_RETRAN_PERIOD                   0x2B                                                                
#define CMD01_GET_CMD_STATUS                      0x2C                          
#define CMD06_GET_REMOT_IPP_SN                    0x2D                                                           
#define CMD10_CLEAR_RECV_BUF_SN                   0x2E                          
#define CMD12_GET_SOCKET_STATUS_SN                0x2F                          
#define CMD11_GET_INT_STATUS_SN                   0x30                              
#define CMD50_SET_IP_ADDR_SN                      0x31                                   
#define CMD30_SET_DES_PORT_SN                     0x32                                  
#define CMD30_SET_SOUR_PORT_SN                    0x33                                 
#define CMD20_SET_PROTO_TYPE_SN                   0x34                                 
#define CMD1W_OPEN_SOCKET_SN                      0x35                          
#define CMD1W_TCP_LISTEN_SN                       0x36                                                                               
#define CMD1W_TCP_CONNECT_SN                      0x37                                                                             
#define CMD1W_TCP_DISNCONNECT_SN                  0x38                                                                              
#define CMD30_WRITE_SEND_BUF_SN                   0x39                                   
#define CMD12_GET_RECV_LEN_SN                     0x3B                                        
#define CMD30_READ_RECV_BUF_SN                    0x3C                                        
#define CMD1W_CLOSE_SOCKET_SN                     0x3D                          
#define CMD20_SET_IPRAW_PRO_SN                    0x3E                                                   
#define CMD01_PING_ENABLE                         0x3F                          
#define CMD06_GET_MAC_ADDR                        0x40                          
#define CMD10_DHCP_ENABLE                         0x41                          
#define CMD01_GET_DHCP_STATUS                     0x42                          
#define CMD014_GET_IP_INF                         0x43                          
#define CMD00_PPPOE_SET_USER_NAME                 0x44                             
#define CMD00_PPPOE_SET_PASSWORD                  0x45                          
#define CMD10_PPPOE_ENABLE                        0x46                          
#define CMD01_GET_PPPOE_STATUS                    0x47                          
#define CMD20_SET_TCP_MSS                         0x50                          
#define CMD20_SET_TTL                             0x51                          
#define CMD30_SET_RECV_BUF                        0x52                            
#define CMD30_SET_SEND_BUF                        0x53                               
#define CMD10_SET_MAC_RECV_BUF                    0x54                            
#define CMD40_SET_FUN_PARA                        0x55                          
#define CMD40_SET_KEEP_LIVE_IDLE                  0x56                    
#define CMD40_SET_KEEP_LIVE_INTVL                 0x57                    
#define CMD10_SET_KEEP_LIVE_CNT                   0x58                    
#define CMD20_SET_KEEP_LIVE_SN                    0X59                    
#define CMD00_EEPROM_ERASE                        0xE9                          
#define CMD30_EEPROM_WRITE                        0xEA                          
#define CMD30_EEPROM_READ                         0xEB                          
#define CMD10_READ_GPIO_REG                       0xEC                          
#define CMD20_WRITE_GPIO_REG                      0xED                          


//----------------------------------------------------
//- ch395 命令状态
//----------------------------------------------------                                                            
#define CMD_ERR_SUCCESS                           0x00                         
#define CMD_RET_ABORT                             0x5F                         
#define CH395_ERR_BUSY                            0x10                         
#define CH395_ERR_MEM                             0x11                         
#define CH395_ERR_BUF                             0x12                         
#define CH395_ERR_TIMEOUT                         0x13                         
#define CH395_ERR_RTE                             0x14                         
#define CH395_ERR_ABRT                            0x15                         
#define CH395_ERR_RST                             0x16                         
#define CH395_ERR_CLSD                            0x17                         
#define CH395_ERR_CONN                            0x18                         
#define CH395_ERR_VAL                             0x19                         
#define CH395_ERR_ARG                             0x1a                         
#define CH395_ERR_USE                             0x1b                         
#define CH395_ERR_IF                              0x1c                         
#define CH395_ERR_ISCONN                          0x1d                         
#define CH395_ERR_OPEN                            0X20                         
#define CH395_ERR_UNKNOW                          0xFA    

//----------------------------------------------------
//- ch395 全局中断状态
//---------------------------------------------------- 
#define GINT_STAT_UNREACH                         (1<<0)     /*不可达中断      */                 
#define GINT_STAT_IP_CONFLI                       (1<<1)     /*IP冲突         */                 
#define GINT_STAT_PHY_CHANGE                      (1<<2)     /*phy状态改变中断 */                   
#define GINT_STAT_DHCP                            (1<<3)     /*dhcp状态改变中断*/                   
#define GINT_STAT_SOCK0                           (1<<4)     /**/                   
#define GINT_STAT_SOCK1                           (1<<5)     /**/                   
#define GINT_STAT_SOCK2                           (1<<6)     /**/                   
#define GINT_STAT_SOCK3                           (1<<7)     /**/                   
#define GINT_STAT_SOCK4                           (1<<8)     /**/                   
#define GINT_STAT_SOCK5                           (1<<9)     /**/                   
#define GINT_STAT_SOCK6                           (1<<10)    /**/                   
#define GINT_STAT_SOCK7                           (1<<11)    /**/    

//----------------------------------------------------
//- ch395 socket中断状态
//----------------------------------------------------                                                  
#define SINT_STAT_SENBUF_FREE                     (1<<0)                        
#define SINT_STAT_SEND_OK                         (1<<1)                        
#define SINT_STAT_RECV                            (1<<2)                        
#define SINT_STAT_CONNECT                         (1<<3)                        
#define SINT_STAT_DISCONNECT                      (1<<4)                        
#define SINT_STAT_TIM_OUT                         (1<<6)             

//----------------------------------------------------
//- 
//---------------------------------------------------- 
#define DEF_KEEP_LIVE_IDLE           (20*1000)//(15*1000)    /*空闲时间20s*/       
#define DEF_KEEP_LIVE_PERIOD         (15*1000)//(20*1000)    /*间隔15s    */         
#define DEF_KEEP_LIVE_CNT            200//200                /*重试200次  */      

void     net_overflow_incr(void);
bool     ch395_init_ch395();
uint8_t  ch395_check_exist(uint8_t datain);
bool     ch395_reset_all();
bool     ch395_open_socket_sn(uint8_t sn);
bool     ch395_close_socket_sn(uint8_t sn);
bool     ch395_tcp_connect_sn(uint8_t sn);
bool     ch395_write_send_buf_sn(uint8_t sn, uint16_t len, uint8_t* pbuf);

uint8_t  ch395_get_ic_ver();
uint8_t  ch395_get_cmd_status();
uint8_t  ch395_get_int_status_sn(uint8_t sn);
uint16_t ch395_get_glob_int_status_all();
uint16_t ch395_get_recv_len_sn(uint8_t sn);
bool     ch395_get_recv_buf_sn(uint8_t sn, uint16_t len, uint8_t* pbuf);

bool     ch395_set_ip_addr(uint8_t* ipaddr);
bool     ch395_set_gwip_addr(uint8_t* ipaddr);
bool     ch395_set_mask_addr(uint8_t* ipaddr);
bool     ch395_set_keep_live_sn(uint8_t sn, uint8_t cmd);
bool     ch395_set_keep_live_cnt(uint8_t cnt);
bool     ch395_set_keep_live_idle(uint32_t idle);
bool     ch395_set_keep_live_intvl(uint32_t intvl);
bool     ch395_set_ip_addr_sn(uint8_t sn, uint8_t *ipaddr);
bool     ch395_set_proto_type_sn(uint8_t sn, uint8_t prototype);
bool     ch395_set_des_port_sn(uint8_t sn, uint16_t port);
bool     ch395_set_sour_port_sn(uint8_t sn, uint16_t port);
bool     ch395_set_ttl(uint8_t sn, uint8_t ttl);

#endif